package com.qing;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * Created by wuliao on 2017/8/23.
 */
public class TestLucene {
    public static void main(String[] args) {


        index();


    }

    private static void search() {
        String path = "/mnt/disk/lucene/";
        FSDirectory directory = null;

        int count = 0;
        try {
            for (int i = 0; i < 10; i++) {
                String str = path + i;
                directory = FSDirectory.open(Paths.get(str));
                StandardAnalyzer analyzer = new StandardAnalyzer();
                IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);
                IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
                count = count + indexWriter.numDocs();
                System.out.println(i + "..." + indexWriter.numDocs());
                indexWriter.commit();
                indexWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("。。。" + count);
    }


    private static void index() {

        String path = "C:\\Users\\Administrator\\Desktop\\index";
        FSDirectory directory = null;
        try {
            directory = FSDirectory.open(Paths.get(path));
            StandardAnalyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(directory, writerConfig);


            Document doc = new Document();
            doc.add(new Field("name", "this is cname", TextField.TYPE_NOT_STORED));
            doc.add(new Field("content", "What is your name?my name is qing.Nice to meet you.", TextField.TYPE_STORED));
            indexWriter.addDocument(doc);
//            indexWriter.commit();
//            indexWriter.close();


            try {
                Thread.sleep(50*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
